package net.goutalk.glcs.module.erpModel.caseErpUnit.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.goutalk.glcs.common.annotation.GlcsLog;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.module.erpModel.caseErpUnit.entity.CaseErpUnitConvert;
import net.goutalk.glcs.module.erpModel.caseErpUnit.entity.CaseErpUnitConvertDetail;
import net.goutalk.glcs.module.erpModel.caseErpUnit.service.ICaseErpUnitConvertDetailService;
import net.goutalk.glcs.module.erpModel.caseErpUnit.service.ICaseErpUnitConvertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import net.goutalk.glcs.module.erpModel.caseErpUnit.dto.AddCaseErpUnitConvertDto;
import net.goutalk.glcs.module.erpModel.caseErpUnit.dto.CaseErpUnitConvertDto;
import net.goutalk.glcs.module.erpModel.caseErpUnit.dto.UpdateCaseErpUnitConvertDto;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping(GlobalConstant.CASE_ERP_UNIT +"/caseErpUnitConvert")
@Api(value = GlobalConstant.CASE_ERP_UNIT +"/caseErpUnitConvert", tags = "单位换算")
@AllArgsConstructor
public class CaseErpUnitConvertController {
    private ICaseErpUnitConvertService caseErpUnitConvertService;

    private ICaseErpUnitConvertDetailService caseErpUnitConvertDetailService;

    @GetMapping(value = "/page")
    @ApiOperation("单位换算配置分页")
    public R page(CaseErpUnitConvertDto dto) {
        return R.ok(caseErpUnitConvertService.getPageList(dto));
    }

    @PostMapping
    @ApiOperation(value = "新增单位换算")
    public R add(@Valid @RequestBody AddCaseErpUnitConvertDto dto) {
        return R.ok(caseErpUnitConvertService.add(dto));
    }

    @PutMapping
    @ApiOperation(value = "编辑单位换算")
    public R update(@Valid @RequestBody UpdateCaseErpUnitConvertDto dto) {
        return R.ok(caseErpUnitConvertService.edit(dto));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询单位换算配置信息")
    public R info(@RequestParam Long id) {
        CaseErpUnitConvert caseErpUnitConvert = caseErpUnitConvertService.getById(id);
        if (caseErpUnitConvert == null) {
            R.error("找不到此单位换算配置信息！");
        }
        return R.ok(caseErpUnitConvertService.getInfo(id));
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @GlcsLog(value = "删除单位换算配置信息")
    public R delete(@Valid @RequestBody Long id) {
        //先删除单位换算表数据
        caseErpUnitConvertService.removeById(id);
        //删除单位换算明细表数据
        caseErpUnitConvertDetailService.remove(Wrappers.<CaseErpUnitConvertDetail>query().lambda().eq(CaseErpUnitConvertDetail::getUnitConvertId, id));
        return R.ok(true);
    }

    @GetMapping("/list-all")
    @ApiOperation("获取所有单位及单位详情")
    public R listAll(){
        return R.ok(caseErpUnitConvertService.listAll());
    }
}
